package cn.us.eagle.system.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.us.eagle.common.Constants;
import cn.us.eagle.query.Query;
import cn.us.eagle.query.criteria.PageQueryCriteria;
import cn.us.eagle.system.model.ConfigData;
import cn.us.eagle.system.service.ConfigDataService;

@Controller
public class ConfigDataController {

	@Resource
	private ConfigDataService configDataService;

	@Resource
	private Query query;
	
    private void setCommonData(Model model) {
        //设置通用属性
    }

	@RequestMapping(value = "administrator/system/configData/list", method = { RequestMethod.GET })
	public String list(Model model) {
		PageQueryCriteria criteria = new PageQueryCriteria();
		criteria.setStrVal1("a");
		criteria.setStrVal2("a");
		List<ConfigData> tmp = configDataService.listAll();
		model.addAttribute("page", tmp);
		model.addAttribute("pageMybatis",
				query.queryForList("ConfigDataMapper.getConfigDatas", criteria));
		model.addAttribute("page2",configDataService.getConfigDataList());
		return "administrator/system/configData/list";
	}
	
	@RequestMapping(value = "administrator/system/configData/add", method = { RequestMethod.GET })
	public String toAdd(Model model){
        if(!model.containsAttribute(Constants.COMMAND)) {
            model.addAttribute(Constants.COMMAND, new ConfigData());
        }
        setCommonData(model);
		return "administrator/system/configData/add";
	}
	
    @RequestMapping(value = "administrator/system/configData/add", method = {RequestMethod.POST})
    public String add(Model model, @ModelAttribute("command") ConfigData command, BindingResult result) {
        
        //如果有验证错误 返回到form页面
        if(result.hasErrors()) {
            model.addAttribute(Constants.COMMAND, command);
            return toAdd(model);
        }
        configDataService.save(command);
        return "redirect:/administrator/system/configData/list";
    }
}
